using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate;
using NHibernate.Linq;

namespace FwkLight.Domain.Query
{
	public abstract class LinqQuery<T>: LinqQuery<T, IList<T>>
	{
		public IList<T> Load(ISession session)
		{
			Initialise(session, false);
			return _queryable.ToList();
		}
	}

  public abstract class LinqQuery<T,TOutput> : IDomainQuery<T,TOutput>
  {
    protected IQueryable<T> _queryable;
    public IDomainQuery<T, TOutput> Initialise(ISession session)
    {
      return Initialise(session, false);
    }

    public IDomainQuery<T, TOutput> Initialise(ISession session, bool includingInactive)
    {
      _queryable = (from item in session.Linq<T>()
                    select item);

      GetMainQuery();
      if(!includingInactive)
        FilterOnlyActive();

      Sort();
      return this;
    }

    protected abstract void GetMainQuery();
    public abstract void FilterOnlyActive ( );
    public abstract void Sort ( );

    public virtual TOutput GetResult()
    {
    	throw new NotImplementedException();
    }
  }
}